/*----------------------------------------------------------
* FILE: EmailHelperImpl.java
* LOGICAL NAME: EmailHelperImpl
* PRODUCT: bila-framework
*----------------------------------------------------------
* MODIFICATIONS
* DATE Oct 10, 2011
* AUTHOR wjuarez
*----------------------------------------------------------
*/

package bila.framework.mail;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 * @author WJuarez
 * <b>Created by </b>wjuarez
 * <b>for</b> bila-framework
 * <b>package</b> bila.framework.mail
 * <b>on</b> Oct 10, 2011 6:08:47 PM
 * <b>Purpose</b> 
 * <p>
 *		Implementacion de EmailHelper
 * </p>
 */
public class EmailHelperImpl implements EmailHelper {

	private JavaMailSender mailSender;
    private MimeMessage message;
    
    /*
     * (non-Javadoc)
     * @see bila.framework.mail.EmailHelper#send(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
	public void send(String to, String from, String fromEmail, String subject, String text) throws Exception {
        message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        helper.setTo(to);
        helper.setFrom(from);
        helper.setSubject(subject);
        helper.setText(text, true); //para definir que el texto sera HTML
        mailSender.send(message);
	}

	/*
	 * (non-Javadoc)
	 * @see bila.framework.mail.EmailHelper#send(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, byte[], java.lang.String, java.lang.String)
	 */
	public void send(String to, String from, String fromEmail, String subject, String text, byte[] bytes, String fileName, String contetType) throws Exception {
        message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);

        helper.setFrom(from);
        helper.setSubject(subject);
        helper.setText(text, true); //para definir que el texto sera HTML

        InputStream attach = new ByteArrayInputStream(bytes);
        StreamAttachmentDataSource datasource = new StreamAttachmentDataSource(attach, fileName, contetType);
        helper.addAttachment(fileName, datasource);

        mailSender.send(message);
	}

	/*
	 * (non-Javadoc)
	 * @see bila.framework.mail.EmailHelper#send(java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	public void send(String to, String[] cc, String from, String fromEmail, 	String subject, String text) throws Exception {
        message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        helper.setTo(to);
        if (cc != null)
            helper.setCc(cc);
        helper.setFrom(from);
        helper.setSubject(subject);
        helper.setText(text, true); //para definir que el texto sera HTML
        mailSender.send(message);
	}

	/*
	 * (non-Javadoc)
	 * @see bila.framework.mail.EmailHelper#send(java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String, java.lang.String, byte[], java.lang.String, java.lang.String)
	 */
	public void send(String to, String[] cc, String from, String fromEmail, String subject, String text, byte[] bytes, String fileName, String contetType) throws Exception {
        message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);
        if (cc != null)
            helper.setCc(cc);
        helper.setFrom(from);
        helper.setSubject(subject);
        helper.setText(text, true); //para definir que el texto sera HTML

        InputStream attach = new ByteArrayInputStream(bytes);
        StreamAttachmentDataSource datasource = new StreamAttachmentDataSource(attach, fileName, contetType);
        helper.addAttachment(fileName, datasource);

        mailSender.send(message);
	}

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}}
